package utils;

import pojo.vo.BookRecordVo;
import service.BookRecordService;
import service.impl.BookRecordServiceImpl;

import java.time.LocalDate;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-12-09
 * Time: 8:27
 */
public class DateThread extends Thread{
    @Override
    public synchronized void run() {
        double fee = 0.5; //一本书逾期一天罚款0.5元
        BookRecordService bookRecordService = new BookRecordServiceImpl();
        //进行逾期提醒
        List<BookRecordVo> bookRecordVoList = bookRecordService.listBookRecord();
        //先根据当前时间 对数据库中的借阅记录逾期栏进行更新
        for (BookRecordVo bookRecordVo: bookRecordVoList){
            LocalDate curTime = TimeFormat.timeFormat();
            //判断当前时间如果早于 归还时间修改逾期状态和罚款金额
            if( curTime.isAfter( bookRecordVo.getReturnTime())){
                Long recordId = bookRecordVo.getId();
                //逾期天数
                long overDay = curTime.toEpochDay()- bookRecordVo.getReturnTime().toEpochDay();
                //罚款金额
                double fineMoney = bookRecordVo.getLoanNumber()*fee*overDay;
                //修改逾期状态
                bookRecordService.setOverDue(recordId);
                //修改罚款金额
                bookRecordService.setFineMoney(fineMoney, recordId);
            }
        }
    }
}
